package www;

import java.awt.event.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.Reader;
import java.io.Writer;
import java.util.Scanner;
import java.util.StringTokenizer;

import javax.swing.*;
public class HandleListener implements ActionListener {
   JTextArea inputText,showText;
   public void setInputText(JTextArea text) {
      inputText = text;
   }
   public void setShowText(JTextArea text) {
      showText = text;
   } 
   public void actionPerformed(ActionEvent e) {
      String str=e.getActionCommand(); 
      if(str.equals("copy"))
         showText.copy();
      else if(str.equals("cut"))
         showText.cut();
      else if(str.equals("paste"))
         inputText.paste();
      else if(str.equals("save")) {
    	  JOptionPane.showMessageDialog(showText, "保存成功", "消息对话框",
                  JOptionPane.WARNING_MESSAGE);
    	  String str1=showText.getText();
          FileWriter writer;
        
          try {
              writer = new FileWriter("ccc.txt");
              writer.write(str1);
              writer.flush();
              writer.close();
          } catch (IOException ee) {
              ee.printStackTrace();
          }}
          else if(str.equals("read")) {
        	  StringBuffer  str1=new StringBuffer("");
        	  FileReader read;
        	  String str2;
        	  
         try {
        	  read = new FileReader("abc.txt");
	           BufferedReader bufferRead =new BufferedReader(read);
	           while((str2=bufferRead.readLine())!=null) {
	        	 // str1.append(str2);
	        	 // str2=bufferRead.readLine();
	        	  showText.setText(str2);
	        	  JOptionPane.showMessageDialog(showText, "读取成功", "消息对话框",
	                      JOptionPane.WARNING_MESSAGE);
	           }
          read.close();
          }
          catch(IOException ee) {ee.printStackTrace();}}
         else if(str.equals("change")) {
       String value=JOptionPane.showInputDialog(showText, "请输入要修改的值", "消息对话框",
               JOptionPane.WARNING_MESSAGE);
        	 String value1= JOptionPane.showInputDialog(showText, "要改成什么", "消息对话框",
                     JOptionPane.WARNING_MESSAGE);
           showText.setText(showText.getText().replace(value, value1));
              }
         else if(str.equals("count")) {
        	 int index=0,imdex=0;
        	 String regex1 = "[A-Za-z]+";
		        String regex2 = "[\u4e00-\u9fa5]+";
        	 String str1=showText.getText();
        	 for(int i=0;i<=str.length();i++) {
        		 char c=str1.charAt(i);
        		 if(Character.toString(c).matches(regex2))
        			 ++index;
		        	
		        		
		        		
        		 else if((c>='a'&&c<='z')) {
						   ++imdex;
						  }
					  
					   
					   }
				 
	      
	          String  ch = " 中文字数:"+index+","+" 英文字数:"+imdex;
	  
	          showText.setText(ch);
	         
         }
    	/*  File fRead = new File("abc.txt");
	      File fWrite = new File("ccc.txt");
	      try{  Writer out = new FileWriter(fWrite);
	            BufferedWriter bufferWrite = new BufferedWriter(out);
	            Reader in = new FileReader(fRead);
	           BufferedReader bufferRead =new BufferedReader(in);
	            String str1 = null;
	            while((str1=bufferRead.readLine())!=null) {
	               StringTokenizer fenxi = new StringTokenizer(str1);
	             int count=fenxi.countTokens();
	               str = str+" �����е��ʸ���:"+count;
	               bufferWrite.write(str1);
	               bufferWrite.newLine();
	            } 
	            bufferWrite.close(); 
	            out.close();
	            in = new FileReader(fWrite);
	            bufferRead =new BufferedReader(in);
	            String s=null;
	            System.out.println(fWrite.getName());
	            while((s=bufferRead.readLine())!=null) {
	              System.out.println(s);
	           }  
	           bufferRead.close();
	           in.close();
	      }
	      catch(IOException ee) {
	          System.out.println(ee.toString());
	      }*/
    	
	   
	}
      
   }
